package com.gzl.autotest.utils;

import com.gzl.autotest.entity.Variable;
import org.apache.log4j.Logger;

import java.util.*;


public class VariableUtil {
    public static Logger logger = Logger.getLogger(VariableUtil.class);

    // 存放变量和值的map
    public static Map<String, String> variableMap = new LinkedHashMap<>();

    // 从对象列表variableList中获取变量和值，放到map中
    public static void loadVariablesToMap(List<Variable> variableList) {
        for (Variable variable : variableList){
            String name = variable.getName();
            String value = variable.getValue();
            variableMap.put(name,value);
        }
    }

    //将字符串parameters中的所有子字符串name替换为value
    public static String variableSubstitution(String parameters) {
        // 获取所有变量名
        Set<String> names = variableMap.keySet();
        for (String name : names) {
            if (parameters.contains(name)){
                parameters = parameters.replace(name,variableMap.get(name));
            }
        }
        return parameters;
    }
}